package com.neardrug.services.impl;

import com.neardrug.entity.example;
import com.neardrug.utils.ChemicalUtil;
import com.neardrug.services.exampleService;
import org.openscience.cdk.exception.CDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rui
 * @ClassName: exampleServiceImpl
 * @create 2023-06-28 16:13
 */
@Service
public class exampleServiceImpl implements exampleService {

    @Autowired
    private com.neardrug.mapper.exampleMapper exampleMapper;

    static List<example> structList = null;

    @Override
    public example findOne(String patentId, int ComNum) {
        return exampleMapper.findOne(patentId, ComNum);
    }

    @Override
    public List<example> searchSS(String smiles) {
        if (structList == null) {
            structList = exampleMapper.findStructure();
        }
        List<example> lst = new ArrayList<>();
        for (example example : structList) {
            try {
                if (example.getCompoundStructure() != null && ChemicalUtil.isSubStructure(smiles, example.getCompoundStructure())) {
                    lst.add(example);
                }
            } catch (CDKException e) {
                e.printStackTrace();
            }
        }
        List<example> res = new ArrayList<>();
        for (example exa : lst) {
            res.add(exampleMapper.findOne(exa.getPatentId(), exa.getComNum()));
        }
        return res;
    }

    @Override
    public List<example> searchSimil(String smiles, Double Threshold) {
        if (structList == null) {
            structList = exampleMapper.findStructure();
        }
        List<example> lst = new ArrayList<>();
        for (example example : structList) {
            try {
                if (example.getCompoundStructure() != null && ChemicalUtil.getSimilarity(smiles, example.getCompoundStructure()) > Threshold) {
                    lst.add(example);
                }
            } catch (CDKException e) {
                e.printStackTrace();
            }
        }
        List<example> res = new ArrayList<>();
        for (example exa : lst) {
            res.add(exampleMapper.findOne(exa.getPatentId(), exa.getComNum()));
        }
        return res;
    }

    @Override
    public List<example> searchMakushi(String smiles) {
        return null;
    }

}
